<template>
  <div class="action-list">
    <el-card class="list-card">
      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :model="searchForm" inline>
          <el-form-item label="用户ID">
            <el-input
              v-model="searchForm.uid"
              placeholder="请输入用户ID"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 表格区域 -->
      <el-table :data="list" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="name" label="操作名称" width="150" />
        <el-table-column prop="url" label="请求URL" min-width="200" show-overflow-tooltip />
        <el-table-column prop="request" label="请求参数" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            <el-text type="info" size="small">{{ row.request }}</el-text>
          </template>
        </el-table-column>
        <el-table-column prop="response" label="响应数据" min-width="200" show-overflow-tooltip>
          <template #default="{ row }">
            <el-text type="success" size="small">{{ row.response }}</el-text>
          </template>
        </el-table-column>
        <el-table-column prop="time" label="操作时间" width="180">
          <template #default="{ row }">
            {{ formatTime(row.time) }}
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页区域 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { getActionList } from "@/api/system";
import { ref } from "vue";
import type { ActionList } from "@/api/types";

const list = ref<ActionList[]>([]);
const loading = ref<boolean>(false);

const total = ref<number>(0);
const page = ref<number>(1);
const size = ref<number>(10);
const uid = ref<number>(1);

// 搜索表单
const searchForm = ref({
  uid: ''
});

// 格式化时间戳
const formatTime = (timestamp: number) => {
  if (!timestamp) return '-';
  const date = new Date(timestamp * 1000); // 假设时间戳是秒级
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  });
};

// 加载数据
const loadData = async () => {
  try {
    loading.value = true;
    const params: any = {
      page: page.value,
      size: size.value,
      uid: uid.value,
    };
    
    // 如果有搜索条件，使用搜索的uid
    if (searchForm.value.uid) {
      params.uid = parseInt(searchForm.value.uid);
    }
    
    const response: any = await getActionList(params);
    list.value = response.data.data;
    total.value = response.data.total;
  } catch (error) {
    console.error('加载数据失败:', error);
  } finally {
    loading.value = false;
  }
};

// 搜索功能
const handleSearch = () => {
  page.value = 1; // 重置到第一页
  loadData();
};

// 重置功能
const handleReset = () => {
  searchForm.value.uid = '';
  page.value = 1;
  loadData();
};

// 分页大小改变
const handleSizeChange = (newSize: number) => {
  size.value = newSize;
  page.value = 1; // 重置到第一页
  loadData();
};

// 当前页改变
const handleCurrentChange = (newPage: number) => {
  page.value = newPage;
  loadData();
};

// 初始化加载数据
loadData();
</script>

<style scoped>
.action-list {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  color: #303133;
  font-size: 20px;
  font-weight: 500;
}

.list-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.search-area {
  margin-bottom: 20px;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 6px;
}

.search-area .el-form {
  margin: 0;
}

.search-area .el-form-item {
  margin-bottom: 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table__header) {
  background-color: #f5f7fa;
}

:deep(.el-table th) {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: 500;
}

:deep(.el-table td) {
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-table__row:hover > td) {
  background-color: #f5f7fa;
}
</style>
